package yt.spider.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import yt.spider.R;
import yt.spider.bean.BookShelfBean;
import yt.spider.help.BookshelfHelp;

/**
 * Created by 小呆呆 on 2018/1/19.
 * RecyclerView的适配器
 */
public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder> {
    private List<BookShelfBean> mList = new ArrayList<>();
    private Context mContext;
    private OnItemClickListener mOnItemClickListener;

    public HomeAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public void removeData(int position) {
        mList.remove(position);
        notifyItemRemoved(position);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(
                mContext).inflate(R.layout.list_item_card, parent,
                false));
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.tv.setText(mList.get(position).getBookInfoBean().getName());
        final View view = holder.mView;
        if (mOnItemClickListener != null) {
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickListener.onItemClick(holder.tv, pos);
                }
            });
            view.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickListener.onItemLongClick(holder.tv, pos);
                    return false;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder
            //对RecyclerView的操作，绑定整个view
    {
        public View mView;
        TextView tv;

        public MyViewHolder(View view) {
            super(view);
            mView = view;
            tv = (TextView) view.findViewById(R.id.tv_name);
        }
    }

    public synchronized void replaceAll(List<BookShelfBean> newDataS, String bookshelfPx) {
        mList.clear();
        if (null != newDataS && newDataS.size() > 0) {
            mList.addAll(newDataS);
            BookshelfHelp.order(mList, bookshelfPx);
        }
        notifyDataSetChanged();
    }
}

